/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Exercicio4.java
 *
 * Created on 10/03/2012, 15:47:38
 */

package posjava2012.aula1.exercicios;

import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Antonio
 */
public class Exercicio4 extends javax.swing.JFrame {

    /** Creates new form Exercicio4 */
    public Exercicio4() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        b3 = new javax.swing.JButton();
        b1 = new javax.swing.JButton();
        b2 = new javax.swing.JButton();
        b4 = new javax.swing.JButton();
        b5 = new javax.swing.JButton();
        b6 = new javax.swing.JButton();
        b7 = new javax.swing.JButton();
        b8 = new javax.swing.JButton();
        b9 = new javax.swing.JButton();
        rbJogador1 = new javax.swing.JRadioButton();
        rbJogador2 = new javax.swing.JRadioButton();
        lResultado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 450));
        getContentPane().setLayout(null);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(110, 140, 200, 10);
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(110, 200, 200, 10);

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator3);
        jSeparator3.setBounds(240, 70, 10, 200);

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator4);
        jSeparator4.setBounds(180, 70, 10, 200);

        b3.setFont(new java.awt.Font("Tahoma", 1, 30));
        b3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b3ActionPerformed(evt);
            }
        });
        getContentPane().add(b3);
        b3.setBounds(240, 80, 60, 60);

        b1.setFont(new java.awt.Font("Tahoma", 1, 30));
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });
        getContentPane().add(b1);
        b1.setBounds(120, 80, 60, 60);

        b2.setFont(new java.awt.Font("Tahoma", 1, 30));
        b2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b2ActionPerformed(evt);
            }
        });
        getContentPane().add(b2);
        b2.setBounds(180, 80, 60, 60);

        b4.setFont(new java.awt.Font("Tahoma", 1, 30));
        b4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b4ActionPerformed(evt);
            }
        });
        getContentPane().add(b4);
        b4.setBounds(120, 140, 60, 60);

        b5.setFont(new java.awt.Font("Tahoma", 1, 30));
        b5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b5ActionPerformed(evt);
            }
        });
        getContentPane().add(b5);
        b5.setBounds(180, 140, 60, 60);

        b6.setFont(new java.awt.Font("Tahoma", 1, 30));
        b6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b6ActionPerformed(evt);
            }
        });
        getContentPane().add(b6);
        b6.setBounds(240, 140, 60, 60);

        b7.setFont(new java.awt.Font("Tahoma", 1, 30));
        b7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b7ActionPerformed(evt);
            }
        });
        getContentPane().add(b7);
        b7.setBounds(120, 200, 60, 60);

        b8.setFont(new java.awt.Font("Tahoma", 1, 30));
        b8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b8ActionPerformed(evt);
            }
        });
        getContentPane().add(b8);
        b8.setBounds(180, 200, 60, 60);

        b9.setFont(new java.awt.Font("Tahoma", 1, 30));
        b9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b9ActionPerformed(evt);
            }
        });
        getContentPane().add(b9);
        b9.setBounds(240, 200, 60, 60);

        buttonGroup1.add(rbJogador1);
        rbJogador1.setFont(new java.awt.Font("Tahoma", 0, 18));
        rbJogador1.setSelected(true);
        rbJogador1.setText("Jogador 1 (X)");
        rbJogador1.setEnabled(false);
        rbJogador1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbJogador1ActionPerformed(evt);
            }
        });
        getContentPane().add(rbJogador1);
        rbJogador1.setBounds(20, 20, 170, 31);

        buttonGroup1.add(rbJogador2);
        rbJogador2.setFont(new java.awt.Font("Tahoma", 0, 18));
        rbJogador2.setText("Jogador 2 (O)");
        rbJogador2.setEnabled(false);
        getContentPane().add(rbJogador2);
        rbJogador2.setBounds(260, 20, 160, 31);

        lResultado.setFont(new java.awt.Font("Tahoma", 1, 18));
        lResultado.setText("Resultado: Indefinido.");
        getContentPane().add(lResultado);
        lResultado.setBounds(10, 290, 410, 40);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rbJogador1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbJogador1ActionPerformed
    }//GEN-LAST:event_rbJogador1ActionPerformed

    private void b2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b2ActionPerformed
        processaBotao((JButton)evt.getSource(), 0, 1);
    }//GEN-LAST:event_b2ActionPerformed

    private void b3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b3ActionPerformed
        processaBotao((JButton)evt.getSource(), 0, 2);
    }//GEN-LAST:event_b3ActionPerformed

    private void b4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b4ActionPerformed
        processaBotao((JButton)evt.getSource(), 1, 0);
    }//GEN-LAST:event_b4ActionPerformed

    private void b5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b5ActionPerformed
        processaBotao((JButton)evt.getSource(), 1, 1);
    }//GEN-LAST:event_b5ActionPerformed

    private void b6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b6ActionPerformed
        processaBotao((JButton)evt.getSource(), 1, 2);
    }//GEN-LAST:event_b6ActionPerformed

    private void b7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b7ActionPerformed
        processaBotao((JButton)evt.getSource(), 2, 0);
    }//GEN-LAST:event_b7ActionPerformed

    private void b8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b8ActionPerformed
        processaBotao((JButton)evt.getSource(), 2, 1);
    }//GEN-LAST:event_b8ActionPerformed

    private void b9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b9ActionPerformed
        processaBotao((JButton)evt.getSource(), 2, 2);
    }//GEN-LAST:event_b9ActionPerformed

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b1ActionPerformed
        processaBotao((JButton)evt.getSource(), 0, 0);
    }//GEN-LAST:event_b1ActionPerformed

    private int[][] mat = new int[3][3];
    boolean vencedor = false;
    int count = 0;
    
    private void processaBotao(JButton b, int l, int c) {
        if (!b.getText().isEmpty()) {
            return;
        }
        if (vencedor) {
            return;
        }
        count++;
        if (rbJogador1.isSelected()) {
            b.setText("X");
            b.setForeground(Color.red);
            rbJogador2.setSelected(true);
            mat[l][c] = 1;
        } else {
            b.setText("O");
            b.setForeground(Color.green);
            rbJogador1.setSelected(true);
            mat[l][c] = 2;
        }
        valida(l, c);
    }

    private void valida(int l, int c) {
        // verifica a linha ou coluna marcada.
        int jogador = mat[l][c];
        // verifica a linha, coluna e Diagonais.
        if (mat[l][0] == mat[l][1] && mat[l][1] == mat[l][2]) {
            // tres iguais na linha.
            vencedor = true;
        } else if (mat[0][c] == mat[1][c] && mat[1][c] == mat[2][c]) {
            // tres iguais na coluna
            vencedor = true;
        } else if (mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2] && mat[1][1] == jogador) {
            // tres iguais na diagonal
            vencedor = true;
        } else if (mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0] && mat[1][1] == jogador) {
            // tres iguais na diagonal
            vencedor = true;
        }

        if (vencedor) {
            lResultado.setText("Resultado: Jogador" + jogador + " é o Vencedor!");
        } else if (count == 9) {
            vencedor = true;
            lResultado.setText("Resultado: Deu Velha!");
        } else {
            lResultado.setText("Resultado: Indefinido.");
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Exercicio4().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b1;
    private javax.swing.JButton b2;
    private javax.swing.JButton b3;
    private javax.swing.JButton b4;
    private javax.swing.JButton b5;
    private javax.swing.JButton b6;
    private javax.swing.JButton b7;
    private javax.swing.JButton b8;
    private javax.swing.JButton b9;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JLabel lResultado;
    private javax.swing.JRadioButton rbJogador1;
    private javax.swing.JRadioButton rbJogador2;
    // End of variables declaration//GEN-END:variables

}
